package xq.gwt.ui.crud;


import xq.gwt.mvc.model.EntityModel;
import xq.gwt.ui.crud.EntitySelectorModel.CancelListener;
import xq.gwt.ui.widgets.entity.EntitySelectedListener;
import xq.gwt.ui.widgets.feedback.FeedbackPresenter;
import xq.gwt.ui.widgets.feedback.FeedbackService;

import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.CloseClickHandler;
import com.smartgwt.client.widgets.events.CloseClientEvent;

public class EntitySelector implements EntitySelectedListener, CancelListener, EntityReceiver, FeedbackPresenter{	
	private EntitySelectorModel selectorModel;
	private EntitySelectedListener selectionListener;
	final Window winModal = new Window();
	private EntityModel prototype;
	private Canvas header;
	private FeedbackPresenter feedbackPresenterSave;
	
	public EntitySelector(EntityModel prototype){		
		this.prototype = prototype;
		init();
	}
	
	public EntitySelector(EntityModel prototype, EntitySelectedListener selectionListener){		
		this.prototype = prototype;
		this.selectionListener = selectionListener;
		init();
	}
	
	public EntitySelector(EntityModel prototype, EntitySelectedListener selectionListener, Canvas header){		
		this.prototype = prototype;
		this.selectionListener = selectionListener;
		this.header = header;
		init();
	}
	
	
	
	public Canvas getHeader() {
		return header;
	}

	public void setHeader(Canvas header) {
		this.header = header;
	}

	public void init(){
		selectorModel = new EntitySelectorModel(this,this);
		selectorModel.getEntities().setPrototype(prototype);
		feedbackPresenterSave = FeedbackService.getFeedbackPresenter();
		FeedbackService.setFeedbackPresenter(this);
	}

	public void show() {		
		winModal.setWidth(1000);
		winModal.setHeight(700);
		winModal.setTitle("Избор на " + prototype.getTitle());
		winModal.setShowMinimizeButton(false);
		winModal.setIsModal(true);
		winModal.setShowModalMask(true);
		winModal.centerInPage();
		winModal.setShowFooter(true);
		winModal.addCloseClickHandler(new CloseClickHandler() {
			@Override
			public void onCloseClick(CloseClientEvent event) {
				close();
			}
		});

		EntitySelectorView presenter = new EntitySelectorView(header);
		presenter.setEntitySelectedListener(selectorModel);
		presenter.setWidth100();
		presenter.setHeight100();		
		presenter.setEntity(selectorModel);		
		winModal.addItem(presenter);
		winModal.show();
	}
	
	@Override
	public void setEntities(EntityModel[] entities){
		selectorModel.getEntities().setValue(entities);
	}
	
	@Override
	public void entitySelected(EntityModel entity) {
		close();
		if (selectionListener != null){
			selectionListener.entitySelected(selectorModel.getSelectedEntity());
		}
		
		
		
	}

	private void close(){
		FeedbackService.setFeedbackPresenter(feedbackPresenterSave);
		winModal.destroy();
	}
	
	@Override
	public void cancel() {
		close();		
	}

	@Override
	public void showFeedback(String feedback) {
		if(this.winModal == null)
			return;
		winModal.setStatus(feedback);		
	}
}
